<?php

namespace app\common\util\plug;

use app\model\Routes;
use app\common\util\base\BaseServer;

class PlugCreateRoutes extends \app\common\util\base\BaseObserver
{

    public function handle()
    {
        $plugInfo = $this->server->getPlugInfo();
        list($controllerName) = explode('.',$plugInfo['admin'][0]);
        $masterRoute = $plugInfo['author'].'_'.$plugInfo['name'].'_'.strtolower($controllerName);
        $masterRoutes = Routes::create([
            'name' => $plugInfo['title'],
            'redirect'  =>  '/'.$masterRoute.'/list',
            'path'  =>  '/'.$masterRoute,
            'meta'  =>  [
                'affix'=>false,
                'title'=>$plugInfo['title'],
                'elIcon'=>$plugInfo['icon'],
                'hidden'=>false,
                'svgIcon'=>'',
                'activeMenu'=>'/'.$masterRoute,
                'alwaysShow'=>false,
                'breadcrumb'=>true,
            ],
        ]);
        foreach ($plugInfo['admin'] as $controller) {
            list($controllerName) = explode('.',$controller);
            $routes = Routes::create([
                'pid'   =>  $masterRoutes->id,
                'name' => $plugInfo['title'].'-'.$controllerName,
                'redirect' => '',
                'componentStr' => 'list/list.vue',
                'path'  =>  '/'.$plugInfo['author'].'_'.$plugInfo['name'].'_'.strtolower($controllerName).'/list',
                'meta'  =>  [
                    'affix'=>false,
                    'title'=>$plugInfo['title'].'-'.$controllerName,
                    'elIcon'=>$plugInfo['icon'],
                    'hidden'=>false,
                    'svgIcon'=>'',
                    'activeMenu'=>'/'.$masterRoute,
                    'alwaysShow'=>false,
                    'breadcrumb'=>true,
                ],
            ]);
        }
        return true;
    }
}